/*
 *  Copyright 2011 Mark Eschbach
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * $HeadURL$
 * $Id$
 */
package com.meschbach.psi.util.rest;

import com.meschbach.psi.PSIException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;

/**
 * A <code>StringEntityBuilder</code> is an <code>EntityBuilder</code>, producing
 * an HTTP entity from a given string.
 * 
 * @author "Mark Eschbach" (meschbach@gmail.com)
 * @since 2.5.0
 * @version 2.5.0
 */
public class StringEntityBuilder implements EntityBuilder {

    /**
     * The <code>entity</code> string contains the content given by the entity
     */
    private String entity;

    /**
     * Constructs a new <code>StringEntityBuilder</code> with the specified entity
     * @param entity is the entity to send
     */
    public StringEntityBuilder(String entity) {
        this.entity = entity;
    }

    /**
     * @see EntityBuilder
     */
    public HttpEntity buildEntity() throws PSIException {
        try {
            return new StringEntity(entity);
        } catch (UnsupportedEncodingException uee) {
            throw new PSIException("Unable to create HTTP entity", uee);
        }
    }
}
